from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_kodeMap
import mapCodeEdit
#import res

class MapCode(QDialog, Ui_kodeMap.Ui_mapCodeDlg):
  def __init__(self, dbConn, dataGlob,  parent=None):
        super(MapCode, self).__init__(parent)
        self.setupUi(self)       
        self.initMenu()
        self.db = dbConn
        self.dataGlob = dataGlob
        self.mastModel = QSqlQueryModel(self)
        self.mastModel.setHeaderData(0, Qt.Horizontal, QVariant("Map Code"))
        self.mastModel.setHeaderData(1, Qt.Horizontal, QVariant("Type"))
        self.mastModel.setHeaderData(2, Qt.Horizontal, QVariant("Description"))
        self.mastModel.setHeaderData(3, Qt.Horizontal, QVariant("Group"))
        
        self.master_tv.setModel(self.mastModel)
        self.master_tv.setSelectionMode(QTableView.SingleSelection)
        self.master_tv.setSelectionBehavior(QTableView.SelectRows)        
        
  @pyqtSignature("")
  def on_desc_le_editingFinished(self):
      if self.desc_le.text() != "":
          self.desc_cb.setCheckState(Qt.Checked)
      else:
          self.desc_cb.setCheckState(Qt.Unchecked)
                  
  def initMenu(self):
      self.retrieveAct = QAction(QIcon(":/retrieve.ico"), self.tr("Retrieve"), self)
#      self.retrieveAct.setShortcut ("Ctrl+R")
      self.connect(self.retrieveAct, SIGNAL("triggered()"), self.retrieveData)
      self.sortAct = QAction(QIcon(":/sort.ico"), self.tr("Sort"), self)
#      self.sortAct.setShortcut ("Ctrl+S")
      self.addAct = QAction(QIcon(":/add.ico"), self.tr("Add"), self)
#      self.addAct.setShortcut ("Ctrl+A")
      self.connect(self.addAct, SIGNAL("triggered()"), self.addData)
      self.editAct = QAction(QIcon(":/edit.ico"), self.tr("Edit"), self)
#      self.editAct.setShortcut ("Ctrl+E")
      self.connect(self.editAct, SIGNAL("triggered()"), self.editData)
      self.delAct = QAction(QIcon(":/del.ico"), self.tr("Delete"), self)
      self.connect(self.delAct, SIGNAL("triggered()"), self.delData)
#      self.delAct.setShortcut ("Ctrl+Del")
      self.printAct = QAction(QIcon("res/print.ico"), self.tr("Print Preview"), self)
#      self.printAct.setShortcut ("Ctrl+P")

  def retrieveData(self):
      qStr = "select kode, (case tipe when 'A' then 'Aktiva' when 'K' then 'Kewajiban' when 'M' then 'Modal' when 'P' then 'Pendapatan' when 'B' then 'Biaya' else 'Lain-Lain' end) as tipenya, desc_map, (case group_map when '1' then 'Aktiva Lancar' when '2' then 'Aktiva Tetap' when '3' then 'Aktiva Tak Berwujud' when '4' then 'Hutang Lancar' when '5' then 'Hutang Tak Lancar' when '6' then 'Modal' else '-' end) as grupnya, tipe, group_map from master_kode_map"
      filt = ""
      urut = " order by kode "
      if self.code_cb.checkState() == Qt.Checked:
          filt = filt + " kode like '"+self.code_le.text()+"%' and "
      if self.desc_cb.checkState() == Qt.Checked:
          filt = filt + " desc_map like '"+self.desc_le.text()+"%' and "
          
      if filt != "":
          filt = " where "+filt.left(len(filt)-4)
          
      qStr = qStr + filt + urut
      self.mastModel.setQuery(qStr, self.db)
      self.master_tv.setColumnWidth(1, 100)
      self.master_tv.setColumnWidth(2, 300)
      self.master_tv.setColumnWidth(3, 300)
      self.master_tv.setColumnHidden(4, True)
      self.master_tv.setColumnHidden(5, True)
      self.master_tv.resizeRowsToContents()
      
  def addData(self):
      form = mapCodeEdit.MapCodeEdit(self.db, "add",  self.dataGlob, self)
      form.show()

  def editData(self):
      index = self.master_tv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("kode").toString()
          form = mapCodeEdit.MapCodeEdit(self.db, id,  self.dataGlob, self)
          form.show()

  def delData(self):
      index = self.master_tv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("kode").toString()
          nama = brs.value("desc_map").toString()
          if QMessageBox.question(self, "Mapping Code","Delete Data : "+nama+"?", QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes:
              qMast = QSqlQuery(self.db)
              self.db.transaction()
              qMast.exec_("delete from master_kode_map where kode = '"+id+"'")
              self.db.commit()
              self.retrieveData()

  def sortData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def printData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def contextMenuEvent(self, event):
        menu = QMenu(self)
        menu.addAction(self.retrieveAct)
        menu.addAction(self.sortAct)
        menu.addSeparator()
        menu.addAction(self.addAct)
        menu.addAction(self.editAct)
        menu.addSeparator()
        menu.addAction(self.delAct)
        menu.addSeparator()
        menu.addAction(self.printAct)
        menu.exec_(event.globalPos())        
            
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = MapCode()
    form.show()
    app.exec_()
